package SocialNetwork.actions;

import java.util.Map;


import Dominion.User;
import Dominion.Author;
import Dominion.Publisher;
import Dominion.Reviewer;


import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class Login extends ActionSupport{
	private static final long serialVersionUID = 1L;
	private String email;
	private String password;

	private User u;

	private String mostrar; 
	private String notifications; 
	
	public String execute() {
		String R; 
		try {
			Map<String, Object> session = ActionContext.getContext().getSession();  // Get session
			
			this.u = new User(); 
			this.u = this.u.log(this.email, this.password);
			R = u.getRol();
			this.notifications = u.get_notifications().toString();	
			u.update_notifications(u.get_notifications());
			this.mostrar = u.toString();

			session.put("loggedin", true);
			if(u.getRol().equals("A")){
				session.put("role", 1);
				Author a = new Author(u);
				u=a;
			}
			else if(u.getRol().equals("P")){
				session.put("role", 2);
				Publisher p = new Publisher(u);
				u=p;
			}
			else if(u.getRol().equals("R")){
				session.put("role", 3);
				Reviewer r = new Reviewer(u);
				u=r;
			}
			session.put("user", u);
			return R;
		}
		catch (Exception e) {
			return ERROR;
		}
	}
	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}
	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	public String getMostrar(){
		return mostrar;
	}
	public String getNotifications(){
		return notifications;
	}
}
